{% extends "auth/base.html" %}

{% block title %}系统日志 - Flask权限管理系统{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><i class="fas fa-list-alt"></i> 系统日志</h2>
        <div>
            <button class="btn btn-outline-danger" id="clearLogsBtn">
                <i class="fas fa-trash"></i> 清空日志
            </button>
        </div>
    </div>
    
    <div class="card">
        <div class="card-header">
            <div class="row">
                <div class="col-md-6">
                    <h5 class="mb-0">日志列表</h5>
                </div>
                <div class="col-md-6">
                    <div class="row">
                        <div class="col-md-4">
                            <select class="form-select" id="logLevelFilter">
                                <option value="">所有级别</option>
                                <option value="INFO">信息</option>
                                <option value="WARNING">警告</option>
                                <option value="ERROR">错误</option>
                                <option value="CRITICAL">严重</option>
                            </select>
                        </div>
                        <div class="col-md-8">
                            <div class="input-group">
                                <input type="text" class="form-control" placeholder="搜索日志..." id="searchInput">
                                <button class="btn btn-outline-secondary" type="button" id="searchBtn">
                                    <i class="fas fa-search"></i>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="card-body">
            {% if logs %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>时间</th>
                            <th>级别</th>
                            <th>用户</th>
                            <th>IP地址</th>
                            <th>消息</th>
                            <th>详情</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for log in logs %}
                        <tr>
                            <td>{{ log.created_at.strftime('%Y-%m-%d %H:%M:%S') }}</td>
                            <td>
                                {% if log.level == 'INFO' %}
                                <span class="badge bg-info">信息</span>
                                {% elif log.level == 'WARNING' %}
                                <span class="badge bg-warning">警告</span>
                                {% elif log.level == 'ERROR' %}
                                <span class="badge bg-danger">错误</span>
                                {% elif log.level == 'CRITICAL' %}
                                <span class="badge bg-dark">严重</span>
                                {% else %}
                                <span class="badge bg-secondary">{{ log.level }}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if log.user %}
                                <a href="{{ url_for('admin.edit_user', user_id=log.user.id) }}">{{ log.user.username }}</a>
                                {% else %}
                                系统
                                {% endif %}
                            </td>
                            <td>{{ log.ip_address or '-' }}</td>
                            <td>{{ log.message }}</td>
                            <td>
                                <button class="btn btn-sm btn-outline-info" data-bs-toggle="collapse" 
                                        data-bs-target="#details-{{ log.id }}" aria-expanded="false">
                                    <i class="fas fa-eye"></i>
                                </button>
                            </td>
                        </tr>
                        <tr class="collapse" id="details-{{ log.id }}">
                            <td colspan="6">
                                <div class="card card-body">
                                    <h6>详细信息</h6>
                                    <pre class="bg-light p-2 rounded">{{ log.details or '无详细信息' }}</pre>
                                    <div class="row mt-2">
                                        <div class="col-md-6">
                                            <strong>用户代理:</strong> {{ log.user_agent or '-' }}
                                        </div>
                                        <div class="col-md-6">
                                            <strong>请求URL:</strong> {{ log.url or '-' }}
                                        </div>
                                    </div>
                                </div>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            
            <!-- 分页 -->
            <nav aria-label="日志列表分页">
                <ul class="pagination justify-content-center">
                    <li class="page-item disabled">
                        <a class="page-link" href="#" tabindex="-1">上一页</a>
                    </li>
                    <li class="page-item active"><a class="page-link" href="#">1</a></li>
                    <li class="page-item"><a class="page-link" href="#">2</a></li>
                    <li class="page-item"><a class="page-link" href="#">3</a></li>
                    <li class="page-item">
                        <a class="page-link" href="#">下一页</a>
                    </li>
                </ul>
            </nav>
            {% else %}
            <div class="text-center py-4">
                <i class="fas fa-list-alt fa-3x text-muted mb-3"></i>
                <h5>暂无日志数据</h5>
                <p class="text-muted">系统中还没有任何日志记录</p>
            </div>
            {% endif %}
        </div>
    </div>
</div>

<!-- 清空日志确认模态框 -->
<div class="modal fade" id="clearLogsModal" tabindex="-1" aria-labelledby="clearLogsModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="clearLogsModalLabel">确认清空日志</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p>您确定要清空所有系统日志吗？</p>
                <p class="text-danger">此操作不可撤销！</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="clearLogsForm" method="post" action="{{ url_for('admin.clear_logs') }}" style="display: inline;">
                    <button type="submit" class="btn btn-danger">确认清空</button>
                </form>
            </div>
        </div>
    </div>
</div>

<script>
    // 清空日志
    document.getElementById('clearLogsBtn').addEventListener('click', function() {
        new bootstrap.Modal(document.getElementById('clearLogsModal')).show();
    });
    
    // 日志级别过滤
    document.getElementById('logLevelFilter').addEventListener('change', function() {
        const level = this.value.toLowerCase();
        const rows = document.querySelectorAll('tbody tr:not(.collapse)');
        
        rows.forEach(row => {
            if (level) {
                const levelCell = row.cells[1].textContent.toLowerCase();
                row.style.display = levelCell.includes(level) ? '' : 'none';
            } else {
                row.style.display = '';
            }
        });
    });
    
    // 搜索功能
    document.getElementById('searchBtn').addEventListener('click', function() {
        const searchTerm = document.getElementById('searchInput').value.toLowerCase();
        const rows = document.querySelectorAll('tbody tr:not(.collapse)');
        
        rows.forEach(row => {
            const text = row.textContent.toLowerCase();
            row.style.display = text.includes(searchTerm) ? '' : 'none';
        });
    });
    
    // 实时搜索
    document.getElementById('searchInput').addEventListener('keyup', function() {
        const searchTerm = this.value.toLowerCase();
        const rows = document.querySelectorAll('tbody tr:not(.collapse)');
        
        rows.forEach(row => {
            const text = row.textContent.toLowerCase();
            row.style.display = text.includes(searchTerm) ? '' : 'none';
        });
    });
</script>
{% endblock %}